package recallin.social.twitter;

import java.util.Date;

import oauth.signpost.OAuth;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.android.R;

public class TwitterDialog extends Dialog implements View.OnClickListener {

	// private LinearLayout onlineFrame;
	private TextView logout;
	private Button tweet;
	private ImageView profilePic;
	private TextView screenName;
	private MultiAutoCompleteTextView completeTextView;
	private ProgressBar tweetLoading;
	private ProgressDialog dialog;
	public static TwitterHandler twitterHandler;

	public TwitterDialog(Context context) {
		super(context);
		this.getWindow().setGravity(Gravity.TOP);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.twitter_dialog);

		twitterHandler = new TwitterHandler();

		logout = (TextView) this
				.findViewById(R.id.twitter_dialog_logout_button);
		tweet = (Button) this.findViewById(R.id.twitter_dialog_tweet);
		profilePic = (ImageView) this
				.findViewById(R.id.twitter_dialog_profile_pic);
		screenName = (TextView) this
				.findViewById(R.id.twitter_dialog_screen_name);
		completeTextView = (MultiAutoCompleteTextView) this
				.findViewById(R.id.twitter_dialog_text_field);
		tweetLoading = (ProgressBar) this
				.findViewById(R.id.twitter_dialog_tweet_loading);

		tweet.setOnClickListener(this);
		logout.setOnClickListener(this);
		twitterHandler.sendEmptyMessage(0);
	}

	public class TwitterHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			int what = msg.what;
			switch (what) {
			case 0:
				profilePic.setImageBitmap(TwitterUtils.getBitmap(TwitterUtils.getProfileImageURL()));
				screenName.setText(TwitterUtils.getScreenName());
				break;
			case 2:
				Toast.makeText(getContext(), "Tweeted", Toast.LENGTH_SHORT)
						.show();
				tweet.setVisibility(Button.VISIBLE);
				tweetLoading.setVisibility(ProgressBar.GONE);
				completeTextView.setText("");
				TwitterDialog.this.dismiss();
				break;
			case 3:
				tweet.setVisibility(Button.GONE);
				tweetLoading.setVisibility(ProgressBar.VISIBLE);
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	}

	private void clearCredentials() {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(getContext());
		final Editor edit = prefs.edit();
		edit.remove(OAuth.OAUTH_TOKEN);
		edit.remove(OAuth.OAUTH_TOKEN_SECRET);
		edit.commit();
	}

	private String getTweetMsg() {
		return "#Recallin Application http://www.Recallin.com "
				+ new Date().toLocaleString();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.twitter_dialog_tweet:
			Thread thread = new Thread() {
				@Override
				public void run() {
					try {
						twitterHandler.sendEmptyMessage(3);
						TwitterUtils.sendTweet(getContext(), completeTextView
								.getText().toString() + "\n" + getTweetMsg());
						twitterHandler.sendEmptyMessage(2);
					} catch (Exception e) {
						e.printStackTrace();
					}
					super.run();
				}
			};
			thread.start();
			break;

		case R.id.twitter_dialog_logout_button:
			clearCredentials();
			dismiss();
			Toast.makeText(getContext(), "Logged out", Toast.LENGTH_SHORT)
					.show();
			twitterHandler.sendEmptyMessage(1);
			break;
		default:
			break;
		}

	}

}
